
#include "loop_module/loop.h"
#include "socket_address.h"
#include "socket_server.h"
#include "socket_server_connection.h"

#include <cstdio>
#include <gtest/gtest.h>

TEST(SockServer, Init)
{
    Loop          loop;
    SocketServer  server(&loop);

    SocketAddress address = SocketAddress::Address("0.0.0.0", 8081);
    server.OpenSocket();
    server.BindAddress(address);
    server.Listen(100);
    std::vector<std::shared_ptr<SocketServerConnection>> connection_vec;
    server.AsyncAccept([&server, &connection_vec](std::shared_ptr<SocketServerConnection> connection) {
        printf("new socket connection.\n");
        connection->AsyncRead(
            [](std::shared_ptr<SocketServerConnection> connn, int result, SocketServerConnection::Message & message) {
                printf("on message result : %d.\n", result);
                if (result >= 0)
                {
                    printf("content : %s.\n", (char *)message.m_data);
                    message.m_use_length = message.m_data_length;
                    connn->Write(message.m_data, message.m_data_length);
                }
                else
                {
                }
            });
        connection_vec.push_back(connection);
    });
    loop.Run();
}
